<template>
<div  class="loginBJ">
  <div style="width: 400px;background-color: rgba(255,255,255,0.3); margin: 50px auto; padding: 30px 60px;">
    <h1 style="text-align: center; margin: 20px 0;">邮箱登录</h1>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
      <el-form-item label="邮箱账号" prop="email" >
        <el-input v-model="ruleForm.email" type="email"placeholder="请输入邮箱地址"></el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="code">
        <el-input v-model="ruleForm.code"placeholder="请输入验证码" class="yzm" ></el-input>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <el-button size='mini' @click="send" v-if="isSend">发送验证码</el-button>
        <span v-if="!isSend" class="count">{{count}}秒后重试</span>

      </el-form-item>
      <el-form-item>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <el-button type="primary"  @click="submitForm('ruleForm')">登录</el-button>
      </el-form-item>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <span><a class="aa" @click="shouji">手机号码登录</a>    |    <a class="aa" @click="zhanghao">用户账号登录</a></span>
    </el-form>
  </div>
</div>
</template>

<script>

export default {

  data() {
    return {
      ruleForm: {
        email:"",
        code:"",
      },
      sendmsg: '发送验证码',
      isSend: true,
      timer: null,
      count: '',
      rules: {
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },/*validator:validateEmail,*/
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 5, max: 5, message: '输入正确位数', trigger: 'blur' }
        ]
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "http://localhost:8080/users/login/email";
          let a=this.qs.stringify(this.ruleForm);
          this.axios.post(url,a).then((response)=>{
            if (response.data.state==20000){
              let jwt=response.data.data;
              localStorage.setItem('jwt', jwt);

              let url1='http://localhost:8080/users/selectByEmail/'+this.ruleForm.email
              this.axios.get(url1).then((resp)=>{
                let arr=resp.data.data;
                let loginInfo={
                  LoginName:arr.username,
                  openId:"asfafsfsfsdfsdfsdfdsfs",
                  role:arr.roleName
                }
                this.cookie.setCookie(loginInfo,1);
                this.$message({
                  message: '登录成功！',
                  type: 'success'
                });
                this.$router.push('/');
              })

            }else {
              this.$message.error(response.data.message);
            }
          })


        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    send() {
      let a=/^\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/;
      if (a.test(this.ruleForm.email) != false) {
        let url = 'http://localhost:8080/users/login/email/send/'+this.ruleForm.email;
        this.axios.get(url).then((response)=> {
          if (response.data.state==20000){
            this.$message({
              message: '验证码发送成功！',
              type: 'success'
            });
            const TIME_COUNT = 60;
            if (!this.timer) {
              this.count = TIME_COUNT;
              this.isSend = false;
              this.timer = setInterval(() => {
                if (this.count > 0 && this.count <= TIME_COUNT) {
                  this.count--;
                } else {
                  this.isSend = true;
                  clearInterval(this.timer);
                  this.timer = null;
                }
              }, 1000)
            }
          }else {
            this.$message.error(response.data.message);
          }
        });



      } else {
        this.$message.error("请填写正确的邮箱后获取验证码！");
      }

    },
    zhanghao() {
      // 跳转到登录页面
      this.$router.push('/login');
    },
    shouji(){
      // 跳转到手机号码登录页面
      this.$router.push('/shoujiLogin');
    },
  }
}
/*var validateEmail = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请正确填写邮箱'));
  } else {
    if (value !== '') {
      var reg=/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
      if(!reg.test(value)){
        callback(new Error('请输入有效的邮箱'));
      }
    }
    callback();
  }
};*/
</script>

<!--<style scoped>-->
<style>
/*body {*/
/* */
/*}*/
.loginBJ{
  height: 100%;
  background-image: url("/src/assets/loginbj.jpeg");
  background-size: cover;
  overflow: hidden;
}
.yzm{
  width: 120px !important;
}
.aa {
  color: #00A0E9;
}
</style>